// pages/paihang/paihang.js
var that=''
Page({

  /**
   * 页面的初始数据
   */
  data: {
  img:'1.jpg',
  age:28,
  beauty:85,
  gender:'女',
  show:false,
  showbutton:true,
  isactive:false,
  imgB64:''
  },
  onLoad: function (options) {
    that = this;
  },
  chooseImg: function () {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths[0];
        that.getB64ByUrl(tempFilePaths);
        that.setData({
          img: tempFilePaths,
          showbutton: false
        });
      }
    })
  },
  getB64ByUrl: function (url) {
    const FileSystemManager = wx.getFileSystemManager();
    FileSystemManager.readFile({
      filePath: url,
      encoding: 'base64',
      success(res) {
        that.setData({
          imgB64: res.data
        });
      }
    })
  },
  beautyTap: function (e) {
    const imgB64 = that.data.imgB64;
    if (!imgB64) {
      wx.showToast({
        title: '请上传图片',
      })
      return;
    };
    that.getToken(function (token) {
      that.getBeauty(token);
    });
  },
  getToken: function (callback) {
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=igEZArYsxvrUNagqMbI8y0dV&client_secret=72SFAKc9r5ihhMI5BGrxCTlCOYYxDMoi',
      success(res) {
        var token = res.data.access_token;
        return callback(token);
      }
    });
  },
  getBeauty:function (token){
        wx.request({
         method:"POST",
          url: "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=" + token,
          data:{
            face_field:'age,beauty,gender',
            image_type:'BASE64',
            image: that.data.imgB64,
            max_face_num:1
          },
          header:{
            'Content-Type':'application/x-www-form-urlencoded'
          },
          success(res)
          {
            if (res.data.error_code==222202)
            {
              wx.showToast({
                title: '图片中没有人脸',
              })
              that.setData({
                showbutton: true
              }) 
              return
            }
             that.setData({
               age: res.data.result.face_list[0].age,
               beauty: res.data.result.face_list[0].beauty,
               gender: res.data.result.face_list[0].gender.type
             })
            that.setData({
              show: true,
              isactive: true,
              showbutton: true
            }) 
          }
         
        })
  },
  getResult:function(){
    var beauty = that.data.beauty
    var gender = that.data.gender
    var gender = (gender=='male')?'帅哥':'美女'
    if (beauty < 30) {
      wx.showModal({
        title: "您好" + gender,
        content: "颜值" + beauty + "您真是不忍直视啊"
      });
    } else if (beauty < 50) {
      wx.showModal({
        title: "您好" + gender,
        content: "颜值" + beauty + "您凑合还可以看"
      });
    } else if (beauty < 80) {
      wx.showModal({
        title: "您好" + gender,
        content: "颜值" + beauty + "您长得很养眼呀"
      });
    }
    else {
      wx.showModal({
        title: "您好" + gender,
        content: "颜值" + beauty + "您真是绝世容颜呀"
      });
    }
    this.setData({ show: false })   
    this.setData({ buttonshow: false })
  },
 
})